package com.x.Part7_创建线程的新方式.线程池;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * ClassName: RunnableTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.03.28 20:07
 */
public class RunnableTest {
    public static void main(String[] args) {

        //
        ExecutorService service1 = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service2 = (ThreadPoolExecutor) service1;

        //限制线程池数量
        service2.setMaximumPoolSize(30);

        service1.execute(new NumThread1());
        service1.execute(new NumThread2());

        service1.shutdown();
    }
}

class NumThread1 implements Runnable{
    @Override
    public void run(){
        for(int i=0;i<10000;i++){
            if(i%2 == 0)
                System.out.println(Thread.currentThread().getName() +":"+ i);
        }
    }
}

class NumThread2 implements Runnable{
    @Override
    public void run(){
        for(int i=0;i<10000;i++){
            if(i%2 != 0)
                System.out.println(Thread.currentThread().getName() +":"+ i);
        }
    }
}